package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * spu信息controller层
 */
@RestController
@RequestMapping("/admin/product")
public class SpuInfoController {

    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 保存spu信息表数据
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //校验数据
        if(null == spuInfo){
            throw new GmallException("参数错误！",null);
        }

        //调用业务层方法
        spuInfoService.save(spuInfo);

        //响应数据
        return Result.ok();
    }

    /**
     * 根据category3Id分页查询spu信息表数据
     * @param category3Id
     * @param pageNum
     * @param size
     * @return
     */
    @GetMapping("/{pageNum}/{size}")
    public Result listPageSpuInfo(@RequestParam("category3Id") Long category3Id,
                                  @PathVariable("pageNum") Long pageNum,
                                  @PathVariable("size") Long size){
        //校验参数
        if(category3Id == null){
            throw new GmallException("参数错误！",null);
        }

        //调用业务层方法
        IPage<SpuInfo> spuInfoIPage = spuInfoService.listPageSpuInfoByCategory3Id(category3Id,pageNum,size);

        //返回结果
        return Result.ok(spuInfoIPage);
    }

}
